<?php 	
	include('connect.php');
	header('Access-Control-Allow-Origin: *');
	header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
	header('Access-Control-Allow-Methods: GET, POST, PUT,DELETE');

	$content = $_POST['content'];
	$tag = $_POST['tag'];
	$index = $_POST['index'];

	// $content = 'a';
	// $tag = 'abc';
	// $index = 'tag5';

	$rows = [];
	$sql = 'update tags set '.$index.'="" where content="'.$content.'"';
	
	$is = $db->query($sql);

	if($is){
		$sql = 'select * from tags where content = "'.$content.'"';
		$result = $db->query($sql);

		$rows = [];
		while($row = $result->fetch_array(MYSQLI_ASSOC)){
			$rows[] = $row;
		}

		//获取非空标签
		$num = sizeof($rows[0])-3;
		$tagAll = array_slice($rows[0], 2, $num);
		$list = array_unique($tagAll);
		$iSNull = in_array('', $list);
		
		if($iSNull){
			$index = array_search('', $list);
			unset($list[$index]);
		}

		$values = [];

		$sql1 = '';
		foreach ($list as $key => $value) {
			array_push($values, $value);
		}
		
		$count1= 5-sizeof($values);
		
		for($y=0;$y<$count1;$y++){
			array_push($values, '');
		}
		
		for($x=0;$x<5;$x++){
			if($x==4){
				$sql1 .= "tag".($x+1)."='".$values[$x]."'";
			}else{
				$sql1 .= "tag".($x+1)."='".$values[$x]."',";
			}
		}

		$sql = 'update tags set '.$sql1.' where content="'.$content.'"';
		
		$db->query($sql);
		$rows = $list;
	}else{
		$rows['code'] = 0;//删除失败
	}

	echo json_encode($rows);
